#ifndef PROGRAM_EDITOR_H
#define PROGRAM_EDITOR_H

#include <QWidget>
#include <QTableWidget>
#include "util/util.h"
#include <QString>
#include <QTime>
#include <QSettings>
#include <QXmlStreamWriter>
#include <iostream>
#include <cctype>


namespace Ui {
class ProgramEditor;
}

class ProgramEditor : public QWidget
{
    Q_OBJECT

public:
    explicit ProgramEditor(QWidget *parent = 0);
    ~ProgramEditor();
    void setProgramName(const QString &programName);
    void changeProgramEditAuthority();
    void detectIfRowCntChange();
    void hideInsertRowButtonDisplay();

signals:
    void returnMenu();
    void RowCountChanged(QTableWidgetItem* item);
    void OrderEdit();
    void sendProgramContent(const QString& programName);

private slots:
    void pushReturnMenu();

private:
    Ui::ProgramEditor *ui;
    QTableWidget *tableWidget;
    SoftKeyboard *softKeyboard;
    int* cellClicked_row;
    int* cellClicked_col;
    int* tableRowCount;
    int* last_tableRowCount;
    int* cellChanged_row;
    QString* lineEditedName;
    safe_thread_queue<QString> CellText_queue;
    QPushButton* clickedIoProgram;

    void insertRow();
    void insertRowUpperLine();
    void insertRowLowerLine();
    void flipInsertRowButtonDisplay();
    void deleteSelectedRow();
    void setTableValue(int row, int column, const QString &value);
    QString getTableValue(int row, int column);
    void saveTableData(QTableWidgetItem* item);
    void setupTableWidget();
    void setupNewTableWidget();
    void connectButtonsAndSignals();
    void findSelectedRow();
    void findSelectedButton();
    void findCancelledButton();
    bool parseCellValue(const char* cellName, int& row, int& col);
    void detectIfCanBeOrderEdited();
    void doOrderEdit();
    void saveOrderEdit();
    void hideOrderEdit();
    bool getChangedCellPos(QTableWidgetItem* item);
    bool judgeIfCellCanBeEdited(QString EditMode);
    void bindTextlineWithSoftkeyBoard();
    void setXml_AxisMove_Move_1(XMLDocument *doc, XMLElement *ele);
    void setOrderEdit_AxisMove_Move_1(XMLDocument* doc, XMLElement* ele);
    void parseFromOrderEdit2Xml();
    void parseAutoProgramFromXml2Cell();
    void parseFromXml2Cell();
    void getCellString_AxisMove_Move_1(XMLElement* ele, int &edited_row);
    void getCellString_Output_ExtractorOut(XMLElement* ele, int &edited_row);
    void displayCellString(int &edited_row);
    void setButtonClickedStyle(QWidget* widget, QPushButton* button);
    void sendProgramToController();
    void setXmlEleOrOrderEdit_AxisMove_Move_1(const QString& setXmlOrOrderEdit, XMLDocument* doc,
                                                             XMLElement* ele, const QString& axis_name,
                                                             QLineEdit* PosData, QLineEdit* VelData);
    bool eventFilter(QObject *watched, QEvent *event);
};

#endif // PROGRAM_EDITOR_H
